package com.example.xui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xui.Main.FirstFragment;
import com.example.xui.Main.ThreeFragment;
import com.example.xui.Way.changStatusIconCollor;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends AppCompatActivity {
    private String suo;
    RecyclerView mRecyclerView;
    MyAdapter mMyAdapter ;
    List<JSONObject> list1=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        changStatusIconCollor changStatusIconCollor=new changStatusIconCollor();
        changStatusIconCollor.changStatusIconCollor1(getWindow(),true);
        suo=getIntent().getStringExtra("suo");
        mRecyclerView=findViewById(R.id.search_recycle);
        Search_for(suo);
    }

    public void Search_for(String suo){
        String str1=suo.substring(0,suo.length()/2);
        String str2=suo.substring(suo.length()/3,suo.length()*3/4);
        String str3=suo.substring(suo.length()/2,suo.length());
        Log.i("tag","str1="+str1);
        Log.i("tag","str1="+str2);
        Log.i("tag","str1="+str3);
        Search_add(str1,str2,str3);
    }

    class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {
        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(getApplicationContext(), R.layout.adapter_note3, null);
             MyViewHoder myViewHoder = new MyViewHoder(view);
            return myViewHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
            if(!list1.equals("[]")) {
                try {
                    Log.i("tag","tag="+list1.get(position).getString("resourcePicture"));
                    holder.pictureto.setImageBitmap(BitmapFactory.decodeFile(list1.get(position).getString("resourcePicture")));
                    //    holder.mTitleContent.setText(list1.get(position).getString("resourceContent"));
                    holder.mTitleid.setText(list1.get(position).getString("resourceNickname"));
                    holder.mTitledTitle.setText(list1.get(position).getString("resourceTitle"));
                    holder.classification.setText(list1.get(position).getString("resourceType"));
                    holder.likenum.setText(list1.get(position).getString("resourceThumbNum"));
                    holder.commentnum.setText(list1.get(position).getString("resourceConmentNum"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public int getItemCount() {
            int a=0;
            //return list1.size();
            if(!list1.equals("[]")){
                a=list1.size();
            }
            return a;
        }
    }
    class MyViewHoder extends RecyclerView.ViewHolder {
        TextView mTitleid;
        //TextView mTitleContent;
        ImageView pictureto;
        TextView mTitledTitle;
        TextView classification;
        TextView likenum;
        TextView commentnum;
        TextView data;

        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
            pictureto = itemView.findViewById(R.id.imageView17);
            mTitleid = itemView.findViewById(R.id.deviceName);
            //   mTitleContent = itemView.findViewById(R.id.deviceAddress);
            mTitledTitle = itemView.findViewById(R.id.textView9);
            classification = itemView.findViewById(R.id.textView47);
            likenum = itemView.findViewById(R.id.textView51);
            commentnum = itemView.findViewById(R.id.textView53);
            data = itemView.findViewById(R.id.textView54);
        }
    }

    public  void Search_add(String a,String b,String c){
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    RequestParams params = new RequestParams("http://10.138.87.49:8080/resource/resoucrceVague");
                    params.addHeader("Content-Type","application/x-www-form-urlencoded");
                    params.addBodyParameter("str1",a);
                    params.addBodyParameter("str2",b);
                    params.addBodyParameter("str3",c);
                    x.http().post(params, new Callback.CacheCallback<JSONArray>() {
                        @Override
                        public void onSuccess(JSONArray result) {
                            List<JSONObject> list=new ArrayList<>();

                            for (int i = 0; i < result.length(); i++) {
                                try {
                                    JSONObject jsonObject = result.getJSONObject(i);
                                    list.add(jsonObject);
                                    //   Thread.sleep(2000);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                                runOnUiThread(new Runnable() {
                                    @Override

                                    public void run() {
                                        list1 = list;
                                        mMyAdapter = new MyAdapter();
                                        mRecyclerView.setAdapter(mMyAdapter);
                                        LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
                                        mRecyclerView.setLayoutManager(layoutManager);
                                        String son = getIntent().getStringExtra("sno");
                                        try {
                                            Log.i("Tag", "tt=" + list1.get(0).getString("resourceID"));
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }

                                });
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            Log.i("dsa",params.toString());
                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }

                        @Override
                        public boolean onCache(JSONArray result) {
                            return false;
                        }
                    });
                }
            }.start();
    }
}
